package com.ems.controller;

import com.ems.config.LogPointer;
import com.ems.entity.UserEntity;
import com.ems.service.IUserService;
import com.ems.util.CommonUtil;
import com.ems.util.RedisUtils;
import com.ems.util.StateEnum;
import com.ems.vo.BaseResult;
import com.ems.vo.UserVo;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2023-10-24
 */
@RestController
@RequestMapping ( "/user" )
@CrossOrigin ( value = "*", maxAge = 3600 )
public class UserController {

    @Resource
    private IUserService iUserService;

    @RequestMapping ( "queryList" )
    @LogPointer ( value = "查询用户列表" )
    public BaseResult queryList ( @RequestBody UserVo userVo ) {
        List<UserVo> userVos = iUserService.queryList ( userVo );
        return BaseResult.success ( userVos, iUserService.queryCount ( userVo ) );
    }

    @RequestMapping ( "getUserList" )
    @LogPointer ( "查询所有用户" )
    public BaseResult getUserList ( @RequestBody UserVo userVo ) {
        List<UserVo> list = iUserService.getList ( userVo );
        return BaseResult.success ( list );
    }

    @RequestMapping ( "saveSingle" )
    @LogPointer ( "新增用户" )
    public BaseResult saveSingle ( @RequestBody UserVo userVo ) {
        UserVo vo = iUserService.querySingle ( userVo );
        if ( vo == null ) {
            vo = iUserService.saveSingle ( userVo );
            return BaseResult.success ( vo );
        }
        return BaseResult.fail ( StateEnum.MOBILE_NOT_UNIQUE );
    }

    @RequestMapping ( "login" )
    @LogPointer ( "登录" )
    public BaseResult login ( @RequestBody UserEntity userEntity ) {
        UserVo userVo = new UserVo ();
        userVo.setTelPhone ( userEntity.getTelPhone () );
        userVo.setPassword ( userEntity.getPassword () );
        UserVo vo = iUserService.querySingle ( userVo );
        if ( vo == null ) {
            return BaseResult.fail ( StateEnum.USER_PASS_IS_NULL );
        }
        String token = CommonUtil.getToken ();
        vo.setToken ( token );
        RedisUtils.set ( token, vo, 7200L, TimeUnit.SECONDS );
        return BaseResult.success ( vo );
    }

    @RequestMapping ( "updateSingle" )
    @LogPointer ( "修改用户" )
    public BaseResult updateSingle ( @RequestBody UserVo userVo ) {
        userVo = iUserService.updateSingle ( userVo );
        return BaseResult.success ( userVo );
    }

    @RequestMapping ( "querySingle" )
    @LogPointer ( "用户详情" )
    public BaseResult querySingle ( @RequestBody UserVo userVo ) {
        UserVo vo = iUserService.querySingle ( userVo );
        if ( vo == null ) {
            return BaseResult.fail ( StateEnum.RESULT_NULL_EXCE );
        }
        return BaseResult.success ( vo );
    }

    @RequestMapping ( "getCurrentUser" )
    @LogPointer ( "获取当前登录用户" )
    public BaseResult getCurrentUser ( @RequestBody UserVo userVo ) {
        UserVo vo = ( UserVo ) RedisUtils.get ( userVo.getToken () );
        return BaseResult.success ( vo );
    }

}
